<?php

namespace App\V3\V3_2_7\Models;

use DateTimeInterface;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;

class OperationLogModel extends Model
{


    use HasFactory;

    protected $table = 'operation_log';

    protected $dateFormat = 'U';//这个时间字段被为被格式化为UNIX 时间戳的形式存储
    //toArray 时间转换问题
    protected function serializeDate(DateTimeInterface $date)
    {
        return $date->format('Y-m-d H:i:s');
    }

    protected $dates = [
        'created_at'
    ];

    static public function getOperatingRecordList($condition,$data){
            if (!empty($data['start_time'])) {
                $startTimeArr = explode(' ', $data['start_time']);
                $startTime = strtotime($startTimeArr[0] . ' 00:00:00');
            }
            if (!empty($data['end_time'])) {
                $endTimeArr = explode(' ', $data['end_time']);
                $endTime = strtotime($endTimeArr[0] . ' 23:59:59');
            }
            $query = SELF::select(
            'nickname',
            'a.remark',
            'log_data',
            'a.created_at'
            )->from('operation_log as a')
            ->leftJoin('admin as b','b.id','=','a.user_id');
            if (!empty($startTime) && !empty($endTime)) {
                $query->whereBetween('a.created_at', [$startTime, $endTime]);
            } else if (!empty($startTime) && empty($endTime)) {
                $query->where('a.created_at', '>=', $startTime);
            } else if (empty($startTime) && !empty($endTime)) {
                $query->where('a.created_at', '<=', $endTime);
            }
    return $query->where([
                'a.delete_flag' =>'0',
                ['log_data','!=','']
            ])->where($condition)
            ->orderBy('a.created_at','desc')
            ->paginate($data['pageSize'],['*'], '',$data['currentPage'])
            ->toArray();

    }
}
